package com.thirtydays.frame.util.ext
/**
 * 返回数据HH:mm:ss
 */
fun Long.formatOutTime(): String {
    var formatTimes = formatTimes()
    return "${formatTimes[1]}:${formatTimes[2]}:${formatTimes[3]}"
}

/**
 * 有小时返回数据没有返回空
 */
fun Long.formatToTime(): String {
    var formatTimes = formatTimes()

    var time = StringBuilder()
    formatTimes.forEachIndexed { index, s ->
        when (index) {
            0, 1, 2 -> {
                if (s != "00") {
                    time.append(s).append(":")
                }
            }
            3 -> {
                time.append(s)
            }
        }
    }

    return time.toString()
}

fun Long.formatMMSS():String{
    var formatTimes = formatTimes()
    return "${formatTimes[2]}:${formatTimes[3]}"
}



fun Long.formatTimes(): MutableList<String> {
    val ss = 1000L
    val mi = ss * 60L
    val hh = mi * 60L
    val dd = hh * 24L

    val day = this / dd
    val hour = (this - day * dd) / hh
    val minute = (this - day * dd - hour * hh) / mi
    val second = (this - day * dd - hour * hh - minute * mi) / ss
    val milliSecond = this - day * dd - hour * hh - minute * mi - second * ss

    val times = mutableListOf<String>()

    if (day > 0) {
        if (day > 9) {
            times.add(day.toString())
        } else {
            times.add("0$day")
        }
    } else {
        times.add("00")
    }

    if (hour > 0) {
        if (hour > 9) {
            times.add("$hour")
        } else {
            times.add("0$hour")
        }
    } else {
        times.add("00")
    }

    if (minute > 0) {
        if (minute > 9) {
            times.add("${minute}")
        } else {
            times.add("0${minute}")
        }
    } else {
        times.add("00")
    }

    if (second > 0) {
        if (second > 9) {
            times.add("$second")
        } else {
            times.add("0${second}")
        }
    } else {
        times.add("00")
    }
    return times
}